package com.shang.sync.demo;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class Demo {
//    private int i = 0;
    //原子类
    private AtomicInteger i = new AtomicInteger(0);
    public static void main(String args[]){
        Demo demo = new Demo();
        //50个线程
        ExecutorService es = Executors.newFixedThreadPool(50);

        CountDownLatch cdl = new CountDownLatch(5000);
        for (int i = 0; i < 5000; i++) {
            es.execute(() -> {
//                demo.i++;
                //CAS操作
                demo.i.getAndIncrement();
                cdl.countDown();
            });
        }
        es.shutdown();
        try {
            cdl.await();
            System.out.println("执行完成,i = " + demo.i);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
